package com.example.shop.exception;

import cn.hutool.crypto.CryptoException;
import com.example.shop.utils.JSONResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;

/**
 * @Description 全局异常处理器
 * @Author by sm
 * @Create 2018-01-08 12:16
 */
@RestControllerAdvice
public class ECSExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 处理自定义异常
     */
    @ExceptionHandler({ECSException.class, IllegalArgumentException.class})
    public JSONResult handleECSException(RuntimeException e) {
        logger.error(e.getMessage(), e);
        return JSONResult.renderError(e.getMessage());
    }

    @ExceptionHandler(CryptoException.class)
    public JSONResult handleCryptoException(CryptoException e) {
        logger.error(e.getMessage(), e);
        return JSONResult.renderError("密码key异常");
    }

    @ExceptionHandler(SQLException.class)
    public JSONResult handleSQLException(SQLException e) {
        logger.error(e.getMessage(), e);
        return JSONResult.renderError("Happen a database access error or other errors.");
    }

    @ExceptionHandler(Exception.class)
    public JSONResult handleException(Exception e) {
        logger.error(e.getMessage(), e);
        return JSONResult.renderError("Server internal error");
    }
}
